package com.xoms.admin.persist.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.xoms.admin.model.ProStockInBean;
import com.xoms.admin.model.ProStockInDetailBean;
import com.xoms.admin.model.vo.AuditStockInBean;
import com.xoms.admin.model.vo.BatchStockInBean;
import com.xoms.admin.model.vo.ProStockInVo;
import com.xoms.admin.persist.dao.ProStockInDao;
import com.xoms.util.StringUtil;
@Repository
public class ProStockInImpl extends BaseImpl implements ProStockInDao {

	final String NAME_SPACE = NAME_SPACE_HEADER + ".ProStockInMapper";
	
	@Override
	public int selectCount(String key, String storeHouseId, String startTime, String endTime) {
		Map<String, Object> params = new HashMap<String, Object>();
		if (!StringUtil.isEmpty(key)) {
			params.put("key", key);
		}
		if(!StringUtil.isEmpty(storeHouseId)) {
			params.put("storeHouseId", storeHouseId);
		}
		if(!StringUtil.isEmpty(startTime)) {
			params.put("startTime", startTime);
		}
		if(!StringUtil.isEmpty(endTime)) {
			params.put("endTime", endTime);
		}
		return sqlSessionTemplate.selectOne(NAME_SPACE +".selectCount", params);
	}

	@Override
	public List<ProStockInBean> selectList(String key, int page, int pagesize,
			String storeHouseId,String startTime,String endTime) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("start", page * pagesize);
		params.put("length", pagesize);
		if (!StringUtil.isEmpty(key)) {
			params.put("key", key);
		}
		if(!StringUtil.isEmpty(storeHouseId)) {
			params.put("storeHouseId", storeHouseId);
		}
		if(!StringUtil.isEmpty(startTime)) {
			params.put("startTime", startTime);
		}
		if(!StringUtil.isEmpty(endTime)) {
			params.put("endTime", endTime);
		}
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectList", params);
	}

	@Override
	public int selectDetailCount(String key, String stockInId) {
		Map<String, Object> params = new HashMap<String, Object>();
		if(!StringUtil.isEmpty(stockInId)) {
			params.put("stockInId", stockInId);
		}
		return sqlSessionTemplate.selectOne(NAME_SPACE +".selectDetailCount", params);
	
	}

	@Override
	public List<ProStockInDetailBean> selectDetailList(String key, int page, int pageSize, String stockInId,
			int paging) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("start", page * pageSize);
		params.put("length", pageSize);
		params.put("paging", paging);
		if(!StringUtil.isEmpty(stockInId)) {
			params.put("stockInId", stockInId);
		}
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectDetailList", params);
	
	}

	@Override
	public String selectMaxNoByTypeInYear(int type) {
		return sqlSessionTemplate.selectOne(NAME_SPACE+ ".selectMaxNoByTypeInYear", type) ;
	}

	@Override
	public int insert(ProStockInBean data) {
		return sqlSessionTemplate.insert(NAME_SPACE + ".insert", data);
	}

	@Override
	public int update(ProStockInBean data) {
		return sqlSessionTemplate.update(NAME_SPACE + ".update", data);
	}

	@Override
	public int del(String sid) {
		return sqlSessionTemplate.delete(NAME_SPACE + ".delete", sid);
	}

	@Override
	public ProStockInBean selectOneById(String sid) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectOneById", sid);
	}

	@Override
	public int replaceBatchDetail(List<BatchStockInBean> batch) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("list", batch);
		return sqlSessionTemplate.insert(NAME_SPACE + ".replaceBatchDetail", params);
	
	}

	@Override
	public int delDetails(String sid) {
		return sqlSessionTemplate.delete(NAME_SPACE + ".delDetails", sid);
	}

	@Override
	public int updateSubmitStatus(String stockInId, int status) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("stockInId", stockInId);
		params.put("status", status);
		return sqlSessionTemplate.update(NAME_SPACE + ".updateSubmitStatus", params);
	
	}

	@Override
	public List<ProStockInVo> selectProductListByIds(String stockInId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("stockInId", stockInId);
		return sqlSessionTemplate.selectList(NAME_SPACE + ".selectProductListByIds", params);
	
	}

	@Override
	public AuditStockInBean selectAuditStockInDetailsById(String aid) {
		return sqlSessionTemplate.selectOne(NAME_SPACE + ".selectAuditStockInDetailsById", aid);
	}

}
